<template>
  <el-form :model="form" label-width="80px">
    
    <el-form-item label="建议内容">
      <el-input 
        v-model="form.comment" 
        type="textarea" 
        :rows="4"
        placeholder="请输入您的建议"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交评价</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, defineEmits } from 'vue'
import * as RatingApi from '@/api/medical/rating'


const props = defineProps({
  doctorId: {
    type: String,
    required: true
  },
  patientId: {  // 修改为patientsId，与父组件传递的prop名称一致
    type: String,
    required: true
  }
})

const emit = defineEmits(['submit'])

const form = reactive({
  doctorId: props.doctorId,
  patientId: props.patientId,
  rating: 5,
  comment: ''
})

const submitForm = async () => {
  if (!form.doctorId || !form.patientId || !form.rating || !form.comment) {
    ElMessage.error('请填写完整的建议')
    console.log(form)
    return
  }
  try {
    const hasRated = await RatingApi.checkRatedExits(parseInt(form.patientId), parseInt(form.doctorId))

    console.log(hasRated)
    if (hasRated) {
      ElMessage.error('您已经对该医生提过建议')
      return
    }
    
    const data = form as unknown as RatingApi.RatingVO
    await RatingApi.createRating(data)
    emit('submit', form)
    
  } catch (error) {
    ElMessage.error('提交失败')
  }
}
</script>